<template>
  <div class="mk-knowledge">
    <div class="swiper-container swiper1">
      <div class="swiper-wrapper">
        <div class="swiper-slide" v-for="(item) in bannerLists">
          <img :src="item.banner_pic" alt>
        </div>
      </div>
      <div class="swiper-pagination"></div>
    </div>
    <div class="bread-nav">
      <div class="nav-box">
        <span>您的位置：</span>
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">网站首页</el-breadcrumb-item>
          <el-breadcrumb-item>环保知识</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
    </div>
    <div class="content">
      <h3 class="title">环保知识</h3>
      <h4 class="sub-title">Environmental knowledge</h4>
      <div class="knowledge">
        <div class="item" v-for="(item,index) in allKnowledge.info">
          <div class="left">
            <h4 class="article-title">{{item.knowledge_name}}</h4>
            <span class="time">{{item.create_time}}</span>
            <p class="text">{{item.knowledge_desc}}</p>
            <div class="more" @click="goKnowledgeDetails(item.id)">查看更多</div>
          </div>
          <div class="right">
            <img :src="'https://cdn1-open.mokyun.com/'+item.knowledge_pic" alt>
          </div>
        </div>
      </div>
      <div class="pages">
        <div class="pages-box">
          <div
            class="page-item"
            v-for="(item,index) in allKnowledge.all_page"
            :class="activeClass==item?'active':''"
            @click="clickItem(item)"
          >{{item}}</div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import getData from "../api/getInfo";

export default {
  data() {
    return {
      bannerLists: [],
      activeClass: 1,
      allKnowledge: []
    };
  },
  mounted() {
    this.getBanner();
    this.getAllKnowledge(1);
  },
  methods: {
    initSwiper() {
      let mySwiper = new Swiper(".swiper1", {
        autoplay: {
          delay: 3000,
          disableOnInteraction: false
        },
        loop: true,
        pagination: {
          el: ".swiper-pagination",
          clickable: true,
          bulletActiveClass: "my-bullet-active"
        },
        observer: true
      });
    },
    //获取banner图
    getBanner() {
      getData
        .getBanner()
        .then(res => {
          let tempArr = [];
          res.data.forEach((val, index) => {
            if (val.type == 4) {
              val.banner_pic = `${getData.url}${val.banner_pic}`;
              tempArr.push(val);
            }
          });
          this.bannerLists = tempArr;
          this.$nextTick(() => {
            // 下一个UI帧再初始化swiper
            this.initSwiper();
          });
        })
        .catch(e => {
          console.log(e);
        });
    },
    //获取所有知识
    getAllKnowledge(page) {
      getData
        .getAllKnowledge(page)
        .then(res => {
          this.allKnowledge = res.data;
        })
        .catch(e => {
          console.log(e);
        });
    },
    //全部知识分页
    clickItem(item) {
      this.activeClass = item;
      this.getAllKnowledge(item);
    },
    //知识详情
    goKnowledgeDetails(knowledge_id) {
      this.$router.push({
        path: `/knowledgeDetails/${knowledge_id}`
      });
    }
  }
};
</script>
<style scoped lang="scss">
.swiper-container {
  height: 505px;
  width: 100%;
}
/deep/.bread-nav {
  width: 100%;
  height: 52px;
  background: #f6f6f6;
  .nav-box {
    width: 1200px;
    height: 52px;
    line-height: 52px;
    margin: 0 auto;
    display: flex;
    span,
    a {
      font-size: 14px;
      color: #9d9d9d;
    }
    .current {
      color: #96ce54;
    }
    .el-breadcrumb {
      line-height: 52px;
    }
    .el-breadcrumb__inner {
      color: #96ce54;
    }
    .el-breadcrumb__inner.is-link {
      color: #9d9d9d;
      font-weight: normal;
    }
  }
}
.content {
  width: 1200px;
  padding-bottom: 100px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  .title {
    font-size: 36px;
    color: #323232;
    margin: 76px 0 6px 0;
  }
  .sub-title {
    font-size: 18px;
    color: #b8b7b7;
    margin-bottom: 70px;
  }
  .knowledge {
    width: 1140px;
    display: flex;
    flex-wrap: wrap;
    .item {
      width: 555px;
      height: 260px;
      box-sizing: border-box;
      border: solid 1px #96ce54;
      display: flex;
      margin-right: 30px;
      margin-bottom: 30px;
      .left {
        width: 345px;
        height: 100%;
        padding: 38px 15px 0 38px;
        box-sizing: border-box;
        .article-title {
          font-size: 18px;
          font-weight: normal;
          color: #96ce54;
        }
        .time {
          display: inline-block;
          margin-top: 6px;
          margin-bottom: 14px;
          font-size: 14px;
          color: #999999;
        }
        .text {
          font-size: 14px;
          line-height: 26px;
          color: #999999;
          text-align: justify;
          min-height: 90px;
        }
        .more {
          width: 112px;
          height: 38px;
          background: #fff;
          border: 1px solid #96ce54;
          text-align: center;
          line-height: 38px;
          color: #96ce54;
          font-size: 16px;
          cursor: pointer;
        }
        .more:hover {
          color: #fff;
          background: #96ce54;
        }
      }
      .right {
        width: 210px;
        height: 100%;
        img {
          width: 100%;
          height: 100%;
        }
      }
    }
    .item:nth-child(2n) {
      margin-right: 0;
    }
  }
  .pages {
    width: 100%;
    height: 50px;
    display: flex;
    justify-content: center;
    .pages-box {
      min-width: 100px;
      height: 50px;
      display: flex;
      .page-item {
        width: 50px;
        height: 50px;
        text-align: center;
        line-height: 50px;
        color: #323232;
        cursor: pointer;
      }
      .page-item.active {
        width: 50px;
        height: 50px;
        background-color: #96ce54;
        text-align: center;
        line-height: 50px;
        color: #fff;
      }
    }
  }
}
</style>

